package client

import (
	"6-17yk/common/proto/user"
	"context"
	"fmt"
	"google.golang.org/grpc"
)

type Handler func(ctx context.Context, client user.UserClient) (interface{}, error)

func UserClient(ctx context.Context, handler Handler) (interface{}, error) {
	conn, err := grpc.Dial("127.0.0.1:8080", grpc.WithInsecure())
	if err != nil {
		fmt.Printf("连接异常： %s\n", err)
	}
	defer conn.Close()
	// 2. 实例化gRPC客户端
	client := user.NewUserClient(conn)
	// 3.组装请求参数
	req, _ := handler(ctx, client)
	return req, nil
}

func SmsSend(ctx context.Context, req *user.SmsSendReq) (*user.SmsSendResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		send, err := client.SmsSend(ctx, req)
		if err != nil {
			return nil, err
		}
		return send, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.SmsSendResp), nil
}
func Login(ctx context.Context, req *user.LoginReq) (*user.LoginResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		send, err := client.Login(ctx, req)
		if err != nil {
			return nil, err
		}
		return send, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.LoginResp), nil
}
func InstitutionList(ctx context.Context, req *user.InstitutionListReq) (*user.InstitutionListResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		send, err := client.InstitutionList(ctx, req)
		if err != nil {
			return nil, err
		}
		return send, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.InstitutionListResp), nil
}
func TeamList(ctx context.Context, req *user.TeamListReq) (*user.TeamListResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		send, err := client.TeamList(ctx, req)
		if err != nil {
			return nil, err
		}
		return send, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.TeamListResp), nil
}
func DoctorList(ctx context.Context, req *user.DoctorListReq) (*user.DoctorListResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		send, err := client.DoctorList(ctx, req)
		if err != nil {
			return nil, err
		}
		return send, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.DoctorListResp), nil
}
func OrderCreate(ctx context.Context, req *user.OrderCreateReq) (*user.OrderCreateResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		send, err := client.OrderCreate(ctx, req)
		if err != nil {
			return nil, err
		}
		return send, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.OrderCreateResp), nil
}
func UserDoctorList(ctx context.Context, req *user.UserDoctorListReq) (*user.UserDoctorListResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		send, err := client.UserDoctorList(ctx, req)
		if err != nil {
			return nil, err
		}
		return send, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.UserDoctorListResp), nil
}
